import sqlite3


class Database:
    TAG = '[SQLite]'

    @staticmethod
    def connect(db_file):
        db_connect = None
        try:
            db_connect = sqlite3.connect(db_file)
        except sqlite3.Error as err:
            print(Database.TAG, f"Error: {err}")
        return db_connect

    @staticmethod
    def query(db_connect, sql, data=None):
        db_cursor = db_connect.cursor()
        if data is not None:
            db_cursor.execute(sql, data)
        else:
            db_cursor.execute(sql)
        result = db_cursor.fetchall()
        # print(Database.TAG, f"Query: {sql}")
        return result

    @staticmethod
    def execute(db_connect, sql, data=None):
        db_cursor = db_connect.cursor()
        if data is not None:
            db_cursor.execute(sql, data)
        else:
            db_cursor.execute(sql)
        db_connect.commit()
        # print(Database.TAG, f"Execute: {sql}")

    @staticmethod
    def executemany(db_connect, sql, data=None):
        db_cursor = db_connect.cursor()
        if data is not None:
            db_cursor.executemany(sql, data)
        else:
            db_cursor.executemany(sql)
        db_connect.commit()
        # print(Database.TAG, f"Execute many: {sql} Count: {db_cursor.rowcount}")
        return db_cursor.rowcount

    @staticmethod
    def close(db_connect):
        db_cursor = db_connect.cursor()
        if db_cursor is not None:
            db_cursor.close()
        if db_connect is not None:
            db_connect.close()
